package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.*;

public class SMRoleDAO {

	private Database db;
	
	public SMRoleDAO(Database db) {
        this.db = db;
    }
	
	public List<Map<String, Object>> findRole(String paSite) {
    	String vaSql = "SELECT role_id, role_name FROM sm_role WHERE site_id = ? ORDER BY role_name ASC";
    	List<Map<String, Object>> result = db.queryList(vaSql, paSite);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
	
	public List<Map<String, Object>> findRoleBySiteId(String paSite) {
    	String vaSql = "SELECT role_id, role_name ,status FROM sm_role WHERE site_id = ? ORDER BY role_id ASC";
    	List<Map<String, Object>> result = db.queryList(vaSql, paSite);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
	
	public List<Map<String, Object>> checkMapping(String paPackageID) 
	  {
		  try
		  {
			  String vaSql = "SELECT * FROM sm_mapping where package_id = ? and role_id = '00'";
			  List<Map<String, Object>> result = db.queryList(vaSql,paPackageID);
			  if (result != null) {
				  return result;
			  } else {
				  return null;
			  }
		  }
		  catch(Exception ex)
		  {
			  return null;
		  }
	  }
	
	public boolean isDuplicate(String paRoleId, String paRoleName) {
        String vaSql = "select * from SM_ROLE where ROLE_ID = ? or ROLE_NAME = ? ";
        Map<String, Object> result = db.querySingle(vaSql,paRoleId, paRoleName);
        if (result != null && result.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
	
	//add sies data
	  public void addRole(SMRole poRole) {
	    	 String vaSql = "INSERT INTO SM_ROLE (ROLE_ID,ROLE_NAME,INSERT_USER_ID,INSERT_DATE,"
	    	 						+ "INSERT_TIME,UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME,SITE_ID,STATUS)"
	    			 				+"VALUES(?,?,?,?,?,?,?,?,?,?)";
	    			try {
	    					db.insert(vaSql, poRole.getVaRoleId(),
	    							poRole.getVaRoleName(), 
	    							poRole.getVnInsertUserId(),
	    							poRole.getVaInsertDate(),
	    							poRole.getVaInsertTime(),
	    							poRole.getVnUpdateUserId(),
	    							poRole.getVaUpdateDate(),
	    							poRole.getVaUpdateTime(),
	    							poRole.getVnSiteId(),
	    							poRole.getVaStatus());
	    				
	    			}catch (Exception e) {
	    	        	throw new RuntimeException(e);
	    			}
	   }
	  
	  public Map<String, Object> findRoleByRoleId(int pnRoleId) {
	        String vaSql = "SELECT * FROM sm_role WHERE role_id = ?";
	        Map<String, Object> result = db.querySingle(vaSql,pnRoleId);
	        if (result != null) {
	            return result;
	        } else {
	            return null;
	        }
	    }
	  
	  public void updateRoleAllAtributes(int pnOldRoleId,SMRole poRole) {
		  
	    	String vaSql  = " UPDATE SM_ROLE SET " +
	    				  " ROLE_NAME = ? , " +
	    				  " INSERT_USER_ID  = ? , " +
	    				  " INSERT_DATE = ? , "+
	    				  " INSERT_TIME = ? , "+
	    				  " UPDATE_USER_ID = ? , "+
	    				  " UPDATE_DATE = ? , "+
	    				  " UPDATE_TIME = ? , "+
	    				  " SITE_ID = ? , "+
	    				  " STATUS = ? "+
	    				  " WHERE " + 
	    				  " ROLE_ID  = ? ";
	    	
	    	 try {
					db.update(vaSql, 
							poRole.getVaRoleName(), 
							poRole.getVnInsertUserId(),
							poRole.getVaInsertDate(),
							poRole.getVaInsertTime(),
							poRole.getVnUpdateUserId(),
							poRole.getVaUpdateDate(),
							poRole.getVaUpdateTime(),
							poRole.getVnSiteId(),
							poRole.getVaStatus(),
							pnOldRoleId);
								
			}catch (Exception e) {
				throw new RuntimeException(e);
			}
	    }
	  
	  public void deleteRole(int pnRoleId)
	  {
		  try
		  {
		    	String vaSql  = " DELETE FROM SM_ROLE " +
	    				  " WHERE ROLE_ID  = ? " ;

		    	db.update(vaSql, pnRoleId);
		  }
		  catch(Exception ex)
		  {
			  throw new RuntimeException(ex);
		  }
	  }
}
